package tools.similarity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Overlap {
	
	public static double getOverlap(String[] str1, String[] str2) {
		Map<String, Integer> count1 = new HashMap<>();
		Map<String, Integer> count2 = new HashMap<>();
		Map<String, Boolean> allWords = new HashMap<>();

		for (String word : str1) {
			if (count1.containsKey(word)) {
				count1.put(word, count1.get(word) + 1);
			} else {
				count1.put(word, 1);
			}
			allWords.put(word, true);
		}
		for (String word : str2) {
			if (count2.containsKey(word)) {
				count2.put(word, count2.get(word) + 1);
			} else {
				count2.put(word, 1);
			}
			allWords.put(word, true);
		}

		int sum = 0;
		for (String word : allWords.keySet()) {
			int c1 = count1.get(word) == null ? 0 : count1.get(word);
			int c2 = count2.get(word) == null ? 0 : count2.get(word);
			sum += c1 * c2;
		}
		
		if (sum == 0) {
			return 0.0;
		}

		double overlap = sum / (str1.length > str2.length ? str2.length : str1.length);

		return overlap;
	}
	
	public static double getOverlap(String[] str1, String[] str2, HashMap<String, Boolean> stopwords) {
		Map<String, Integer> count1 = new HashMap<>();
		Map<String, Integer> count2 = new HashMap<>();
		Map<String, Boolean> allWords = new HashMap<>();

		for (String word : str1) {
			if (stopwords.containsKey(word)) {
				continue;
			}
			if (count1.containsKey(word)) {
				count1.put(word, count1.get(word) + 1);
			} else {
				count1.put(word, 1);
			}
			allWords.put(word, true);
		}
		for (String word : str2) {
			if (stopwords.containsKey(word)) {
				continue;
			}
			if (count2.containsKey(word)) {
				count2.put(word, count2.get(word) + 1);
			} else {
				count2.put(word, 1);
			}
			allWords.put(word, true);
		}

		int sum = 0;
		for (String word : allWords.keySet()) {
			int c1 = count1.get(word) == null ? 0 : count1.get(word);
			int c2 = count2.get(word) == null ? 0 : count2.get(word);
			sum += c1 * c2;
		}
		
		if (sum == 0) {
			return 0.0;
		}

		double overlap = sum / (str1.length > str2.length ? str2.length : str1.length);

		return overlap;
	}
	
	public static double getOverlap(int a, int b, ArrayList<Map<String, Integer>> allSen) {
		Map<String, Integer> count1 = allSen.get(a);
		Map<String, Integer> count2 = allSen.get(b);
		Map<String, Boolean> allWords = new HashMap<>();
		int str1Length = 0;
		int str2Length = 0;
		
		for (String word : count1.keySet()) {
			allWords.put(word, true);
			str1Length = str1Length + count1.get(word);
		}
		for (String word : count2.keySet()) {
			allWords.put(word, true);
			str2Length = str2Length + count2.get(word);
		}
		
		int sum = 0;
		for (String word : allWords.keySet()) {
			int c1 = count1.get(word) == null ? 0 : count1.get(word);
			int c2 = count2.get(word) == null ? 0 : count2.get(word);
			sum += c1 * c2;
		}
		
		if (sum == 0) {
			return 0.0;
		}

		double overlap = sum / (str1Length > str2Length ? str2Length : str1Length);

		return overlap;
	}
}
